package com.purchase.ml.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.purchase.ml.bean.page.Page;
import com.purchase.ml.bean.req.sys.SysRolePermQueryDto;
import com.purchase.ml.bean.req.sys.SysRolePermissionDto;
import com.purchase.ml.entity.sys.SysPermission;
import com.purchase.ml.entity.sys.SysRolePermission;
import com.purchase.ml.mapper.sys.SysPermissionMapper;
import com.purchase.ml.mapper.sys.SysRolePermissionMapper;
import com.purchase.ml.service.sys.ISysRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限对应关系表 服务实现类
 * </p>
 *
 * @author kyyang
 * Created 2021-01-07
 */
@Service
@Slf4j
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public Page<SysPermission> sysRolePermission(SysRolePermQueryDto sysRolePermQueryDto) {
        IPage<SysRolePermission> p = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(sysRolePermQueryDto.getPageIndex(),sysRolePermQueryDto.getPageNum());
        QueryWrapper<SysRolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("roleId",sysRolePermQueryDto.getRoleId());
        Integer integer = sysRolePermissionMapper.selectCount(wrapper);
        Page<SysPermission> page = new Page<>(sysRolePermQueryDto.getPageIndex(),sysRolePermQueryDto.getPageNum());
        if(integer<1){
            return page;
        }
        IPage<SysRolePermission> sysRolePermissionIPage = sysRolePermissionMapper.selectPage(p, wrapper);
        List<SysRolePermission> records = sysRolePermissionIPage.getRecords();
        List<Integer> list  = records.stream().map(SysRolePermission::getPermissionId).collect(Collectors.toList());
        log.info("list=========={}",list);
        List<SysPermission> sysPermissions = sysPermissionMapper.selectBatchIds(list);
        log.info("sysPermissions=========={}",sysPermissions);
        page.setTotal(integer);
        page.setRecords(sysPermissions);
        return page;
    }

    @Override
    @Transactional
    public int sysRolePermissionNew(SysRolePermissionDto sysRolePermissionDto) {
        List<Integer> permissionIds = sysRolePermissionDto.getPermissionIds();
        if(permissionIds.size()>0){
            for(Integer id :permissionIds){
                SysRolePermission sys = new SysRolePermission();
                sys.setRoleId(sysRolePermissionDto.getRoleId());
                sys.setPermissionId(id);
                SysRolePermission s = sysRolePermissionMapper.selectByKey(id,sysRolePermissionDto.getRoleId());
                if(s==null){
                    sysRolePermissionMapper.insert(sys);
                }
            }
        }
        return 0;
    }

    @Override
    @Transactional
    public int sysRolePermissionDel(SysRolePermissionDto sysRolePermissionDto) {
        List<Integer> permissionIds = sysRolePermissionDto.getPermissionIds();
        if(permissionIds.size()>0){
            for(Integer id :permissionIds){
                QueryWrapper<SysRolePermission> wrapper = new QueryWrapper<>();
                wrapper.eq("roleId",sysRolePermissionDto.getRoleId());
                wrapper.eq("permissionId",id);
                sysRolePermissionMapper.delete(wrapper);
            }
        }
        return 0;
    }
}
